package com.lsm.sm.service.impl;

import javax.servlet.ServletContext;
import org.apache.commons.lang.xwork.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.lsm.sm.service.ManagerService;
import com.lsm.sm.util.Const;
import com.lsm.sm.util.WebUtil;

@Scope("prototype")
@Service("managerService")
public class ManagerServiceImpl implements ManagerService{
	/*
	 * 1. 默认调节进度 +1
	 * 2. 调节进度
	 * 2. 设置新课程 course，默认其 schedul = 1;
	 * 3. 重置课程与进度 course，
	 */
	private static final long serialVersionUID = 7333586252715205600L;

	public void deployCourse( int type, String course, int schedule ) throws Exception{
		switch ( type ) {
			case 1:
				deploySchedule( schedule );
				break;
			case 2:
				deployeCourse( course, schedule );
				break;
			default:
				throw new Exception( Const.EXPT_008 );
		}
	}

	private void deploySchedule( int schedule ){
		ServletContext sc = WebUtil.getServletContext();
		if( schedule==1 ){//==1 即默认方式
			Integer preSch = (Integer)sc.getAttribute( Const.SCHEDUL );
			schedule +=preSch.intValue();
		}
		sc.setAttribute( Const.SCHEDUL, Integer.valueOf( schedule ) );
	}
	
	private void deployeCourse( String course, int schedule ) throws Exception {
		ServletContext sc = WebUtil.getServletContext();
		if( StringUtils.isBlank( course ) ){
			throw new Exception( Const.EXPT_009 );
		}
		sc.setAttribute( Const.COURSE, course );
		sc.setAttribute( Const.SCHEDUL, Integer.valueOf(0) );
		deploySchedule( schedule );	//再分配进度
	}
}
